package com.xiaoxi.logserver.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoxi.domain.log.BMSLogLogin;
import com.xiaoxi.logserver.domain.dto.BMSLogLoginDTO;
import com.xiaoxi.logserver.domain.vo.BMSLogLoginVO;
import com.xiaoxi.logserver.mapper.BmsLogLoginMapper;
import com.xiaoxi.logserver.service.BmsLogLoginService;
import com.xiaoxi.service.circulation.ExportDataToExcelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName BmsLogLoginServiceImpl
 * @Description
 * @Author xiaoxi
 * @Date 2022-10-30
 * @Version 1.0
 */
@Service("bmsLogLoginServiceImpl")
@Slf4j
public class BmsLogLoginServiceImpl extends ServiceImpl<BmsLogLoginMapper, BMSLogLogin> implements BmsLogLoginService, ExportDataToExcelService {

    @Override
    public IPage<BMSLogLoginVO> findListByPage(BMSLogLoginDTO bmsLogLoginDTO, Pageable pageable) {

        IPage<BMSLogLogin> iPage = new Page<>(pageable.getPageNumber(), pageable.getPageSize());

        QueryWrapper<BMSLogLogin> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(bmsLogLoginDTO.getUsername())) {
            queryWrapper.like("username", bmsLogLoginDTO.getUsername());
        }
        if (Objects.nonNull(bmsLogLoginDTO.getBeginTime()) && Objects.nonNull(bmsLogLoginDTO.getEndTime())) {
            queryWrapper.between("login_time", bmsLogLoginDTO.getBeginTime(), bmsLogLoginDTO.getEndTime());
        }
        iPage = baseMapper.selectPage(iPage, queryWrapper);

        List<BMSLogLoginVO> bmsLogLoginVOList = new ArrayList<>();
        iPage.getRecords().forEach(BMSLogLogin -> {
            BMSLogLoginVO bmsLogLoginVO = new BMSLogLoginVO();
            BeanUtils.copyProperties(BMSLogLogin, bmsLogLoginVO);
            bmsLogLoginVOList.add(bmsLogLoginVO);
        });
        IPage<BMSLogLoginVO> bmsLogLoginVOIPage = new Page<>(pageable.getPageNumber(), pageable.getPageSize());
        bmsLogLoginVOIPage.setRecords(bmsLogLoginVOList);
        bmsLogLoginVOIPage.setTotal(iPage.getTotal());

        return bmsLogLoginVOIPage;
    }

    @Override
    public void add(BMSLogLogin bmsLogLogin) {
        baseMapper.insert(bmsLogLogin);
    }

    @Override
    public void delete(Long id) {
        baseMapper.deleteById(id);
    }

    @Override
    public void update(BMSLogLogin bmsLogLogin) {
        baseMapper.updateById(bmsLogLogin);
    }

    @Override
    public BMSLogLogin findById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public List<String> tableHeader(String... args) {
        List<String> tableHeader = new ArrayList<>();

        tableHeader.add("序号");
        tableHeader.add("用户名");
        tableHeader.add("密码");
        tableHeader.add("访问ip地址");
        tableHeader.add("登录时间");
        tableHeader.add("返回数据");
        tableHeader.add("登录日志");
        tableHeader.add("返回状态码");
        tableHeader.add("登录状态");

        return tableHeader;
    }

    @Override
    public <T> IPage<T> tableContent(T objDTO, Pageable pageable, String... args) {
        return (IPage<T>) findListByPage((BMSLogLoginDTO) objDTO, pageable);
    }
}
